package service

import (
	"singo/model"
	"singo/serializer"
)

// ProjectShowService 项目详情的服务
type ProjectShowService struct {
}

// ProjectShow 项目
func (service *ProjectShowService) ProjectShow(uid string) serializer.Response {
	var project model.Project

	task_total := 0
	task_done := 0
	demand_total := 0
	demand_done := 0
	defect_total := 0
	defect_done := 0
	overdue_total := 0

	err := model.DB.First(&project, "uid = ?", uid).Error
	if err != nil {
		return serializer.Response{
			Status: 404,
			Msg:    "项目不存在",
			Error:  err.Error(),
		}
	}

	// 项目管理任务数统计
	if err := model.DB.Model(&model.Task{}).Where("project_id = ?", uid).Count(&task_total).Error; err != nil {
		return serializer.Response{
			Status: 50000,
			Msg:    "数据库连接错误",
			Error:  err.Error(),
		}
	}
	if err := model.DB.Model(&model.Task{}).Where("status = ? AND project_id = ?", 3, uid).Count(&task_done).Error; err != nil {
		return serializer.Response{
			Status: 50000,
			Msg:    "数据库连接错误",
			Error:  err.Error(),
		}
	}

	// 项目管理需求数统计
	if err := model.DB.Model(&model.Demand{}).Where("project_id = ?", uid).Count(&demand_total).Error; err != nil {
		return serializer.Response{
			Status: 50000,
			Msg:    "数据库连接错误",
			Error:  err.Error(),
		}
	}
	if err := model.DB.Model(&model.Demand{}).Where("status = ? AND project_id = ?", 3, uid).Count(&demand_done).Error; err != nil {
		return serializer.Response{
			Status: 50000,
			Msg:    "数据库连接错误",
			Error:  err.Error(),
		}
	}

	// 项目管理缺陷数统计
	if err := model.DB.Model(&model.Defect{}).Where("project_id = ?", uid).Count(&defect_total).Error; err != nil {
		return serializer.Response{
			Status: 50000,
			Msg:    "数据库连接错误",
			Error:  err.Error(),
		}
	}
	if err := model.DB.Model(&model.Defect{}).Where("status = ? AND project_id = ?", 3, uid).Count(&defect_done).Error; err != nil {
		return serializer.Response{
			Status: 50000,
			Msg:    "数据库连接错误",
			Error:  err.Error(),
		}
	}
	manageProject := model.ManageProject{
		UID:  uid,
		Name: project.Name,
		Head: project.Head,
		Task: model.TotalInterface{
			Total: task_total,
			Done:  task_done,
		},
		Demand: model.TotalInterface{
			Total: demand_total,
			Done:  demand_done,
		},
		Defect: model.TotalInterface{
			Total: defect_total,
			Done:  defect_done,
		},
		Overdue: overdue_total,
	}

	return serializer.Response{
		Data: serializer.BuildProjectManage(manageProject),
	}
}
